﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace RL
{
    /// <summary>
    /// Логика взаимодействия для ucSelectPerson.xaml
    /// </summary>
    public partial class ucSelectPerson : UserControl
    {
        private routelistEntities db;
        private bool disablesearch = true;
        public routelistEntities DBContext
        {
            get { return db; }
            set { db = value; }
        }



        public person Person
        {
            get { return (person)GetValue(PersonProperty); }
            set
            { 
                SetValue(PersonProperty, value); 
            }
        }



        public driver Driver
        {
            get { return (driver)GetValue(DriverProperty); }
            set { SetValue(DriverProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Driver.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DriverProperty =
            DependencyProperty.Register("Driver", typeof(driver), typeof(ucSelectPerson), new UIPropertyMetadata(null, new PropertyChangedCallback(propDriver)));

        public static void propDriver(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ucSelectPerson item = d as ucSelectPerson;
            if (null == item) return;
            driver it = e.NewValue as driver;
            if (null == it) return;
            item.disablesearch = true;
            item.textBox1.Text = it.person.fio;
            item.disablesearch = true;
        }

        // Using a DependencyProperty as the backing store for Person.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PersonProperty =
            DependencyProperty.Register("Person", typeof(person), typeof(ucSelectPerson), new UIPropertyMetadata(null, new PropertyChangedCallback(propPerson)));

        public static void propPerson(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ucSelectPerson item = d as ucSelectPerson;
            if (null == item) return;
            person it = e.NewValue as person;
            if (null == it) return;
            item.disablesearch = true;
            item.textBox1.Text = it.fio;
            item.disablesearch = true;
        }



        public int? PersonID
        {
            get { return (int?)GetValue(PersonIDProperty); }
            set { SetValue(PersonIDProperty, value); }
        }

        // Using a DependencyProperty as the backing store for PersonID.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PersonIDProperty =
            DependencyProperty.Register("PersonID", typeof(int?), typeof(ucSelectPerson), new UIPropertyMetadata(null, new PropertyChangedCallback(propPersonID)));
        public static void propPersonID(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ucSelectPerson item = d as ucSelectPerson;
            if (null == item) return;
            int? val = (int?)e.NewValue;
            person it = (from o in item.DBContext.person where o.id == val select o).FirstOrDefault();
            item.Person = it;
            if (null == item.Person)
            {
                item.textBox1.Text = "<не указан>";
            } else
            {
                item.textBox1.Text = it.fio;
            }
        }



        public int? DriverID
        {
            get { return (int?)GetValue(DriverIDProperty); }
            set { SetValue(DriverIDProperty, value); }
        }

        // Using a DependencyProperty as the backing store for DriverID.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DriverIDProperty =
            DependencyProperty.Register("DriverID", typeof(int?), typeof(ucSelectPerson), new UIPropertyMetadata(null, new PropertyChangedCallback(propDriverID)));

        public static void propDriverID(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ucSelectPerson item = d as ucSelectPerson;
            if (null == item) return;
            if (null == item.db) item.db = new routelistEntities();
            int? val = (int?)e.NewValue;
            driver it = (from o in item.DBContext.driver where o.id == val select o).FirstOrDefault();
            item.Driver = it;
            try
            {
                item.PersonID = it.person.id;
            }
            catch (Exception)
            {
            }
            item.textBox1.Text = it == null ? "<не указан>" : it.person.fio ;
        }

        


        public ucSelectPerson()
        {
            InitializeComponent();
            textBox1.Text = "<не указан>";
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                return;
            }
            if (null == db) db = new routelistEntities();
            if (null != Person)
            {
                textBox1.Text = String.Format("({0}) {1}", Person.tabel, Person.fio);
            }
        }

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (true == disablesearch) return;
            SelectPerson();
        }

        private void SelectPerson()
        {
            if (null == db) return;
            wPersonSelect dlg = new wPersonSelect();
            //dlg.DBContext = db;
            dlg.PersonID = Person.id;
            dlg.FilterString = textBox1.Text.Trim();
            if (dlg.ShowDialog() == true)
            {
                Person = dlg.SelectedPerson;
            }
        }

        private void buttonClear_Click(object sender, RoutedEventArgs e)
        {
            //textBox1.Text = "";
            Person = null;
            PersonID = null;
            Driver = null;
            DriverID = null;
        }

        private void buttonSelectFromReestr_Click(object sender, RoutedEventArgs e)
        {
            if (null == db) return;
            wPersonSelect dlg = new wPersonSelect();
            //dlg.DBContext = db;
            dlg.PersonID = Person == null ? 0 : Person.id;
            if (dlg.ShowDialog() == true)
            {
                PersonID = dlg.SelectedPerson == null ? 0 : dlg.SelectedPerson.id;

                try
                {
                    driver drv = (from o in DBContext.driver where o.id_person == Person.id select o).FirstOrDefault();
                    
                        Driver = drv;
                        DriverID = drv.id;
                    
                } catch(Exception)
                {
                }
            }
        }
    }

    [ValueConversion(typeof(int), typeof(String))]
    public class DriverIDConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (null == value) return "<не выбран>";
            int item_i = (int)value;
            routelistEntities db = new routelistEntities();
            driver item = (from o in db.driver where o.id == item_i select o).FirstOrDefault();
            if (null == item) return "<не выбран>";
            string result = item.person == null ? "<сотрудник не указан>" : string.Format("({0}) {1}", item.person.tabel.Trim(), item.person.fio.Trim());
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
